package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IserveService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class IserveServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IserveService {


    @Resource
    ServeItemMapper serveItemMapper;

    @Resource
    RegionMapper regionMapper;

    /**
     * @description: 区域服务列表
     * @author: xushuaihang
     * @Date: 2024/8/16 16:02
     * @param:
     * @param servePageQueryReqDTO
     * @return: com.jzo2o.common.model.PageResult<com.jzo2o.foundations.model.dto.response.ServeResDTO>
    **/
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        //通过baseMapper调用queryServeListByRegionId方法
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    /**
     * @description: 区域服务添加
     * @author: xushuaihang
     * @Date: 2024/8/16 16:03
     * @param:
     * @param serveUpsertReqDTOList
     * @return: void
    **/
    @Override
    public void add(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {

        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {

            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());

            if(ObjectUtils.isEmpty(serveItem) || serveItem.getActiveStatus() == FoundationStatusEnum.DISABLE.getStatus()){

              throw  new ForbiddenOperationException("该服务未启用无法添加到区域下使用");

            }

            //校验是否将重复新增
            Integer count = lambdaQuery().eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .count();

            if (count > 0) {
                throw new ForbiddenOperationException(serveItem.getName() + "服务已存在");
            }

            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);

            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());

            serve.setCityCode(region.getCityCode());

            serve.setCreateTime(LocalDateTime.now());

            int insert = baseMapper.insert(serve);

            System.out.println("添加成功！~"+insert);

        }


    }

    /**
     * @description: 区域服务价格修改
     * @author: xushuaihang
     * @Date: 2024/8/16 16:22
     * @param:
     * @param id
     * @param price
     * @return: com.jzo2o.foundations.model.domain.Serve
    **/
    @Override
    public Serve updatePrice(Long id, BigDecimal price) {

        boolean update = lambdaUpdate().set(Serve::getPrice, price)
                .eq(Serve::getId, id).update();

        if(!update){
            throw new CommonException("修改服务价格失败");
        }
        return baseMapper.selectById(id);
    }


    /**
     * @description: 区域服务上架
     * @author: xushuaihang
     * @Date: 2024/8/16 16:22
     * @param:
     * @param id
     * @return: void
    **/
    @Override
    public void onSale(Long id) {

        Serve serve = baseMapper.selectById(id);

        if(serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()){
            System.out.println("该服务已开启");
            throw  new CommonException("该服务已开启！");
        }

        Region region = regionMapper.selectById(serve.getRegionId());

        if(region.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            System.out.println("区域服务未开启");
           throw  new CommonException("区域服务未开启！");
        }

        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if(serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            System.out.println("服务项未开启");
            throw  new CommonException("服务项未开启！");
        }

        boolean update = lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id).update();

        if(!update){
            System.out.println("区域服务开启失败");
            throw  new CommonException("区域服务开启失败！");
        }

    }

    /**
     * @description: 删除区域服务
     * @author: xushuaihang
     * @Date: 2024/8/16 16:52
     * @param:
     * @param id
     * @return: void
    **/
    @Override
    public void remove(Long id) {

        Serve serve = baseMapper.selectById(id);
        if(ObjectUtils.isEmpty(serve)){
            throw new ForbiddenOperationException("该区域服务不存在");
        }

        //  //3.判断状态 是否是 非 （上架状态） ,直接返回
        if(serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("草稿状态才能删除");
        }

        int del = baseMapper.deleteById(id);

        if(del < 0){
            throw new ForbiddenOperationException("删除失败");
        }


    }
}
